﻿<!DOCTYPE html>
<html>

<head lang="en">
    <meta charset="UTF-8">
    <title>注册</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta name="format-detection" content="telephone=no">
    <meta name="renderer" content="webkit">
    <meta http-equiv="Cache-Control" content="no-siteapp"/>

    <link rel="stylesheet" href="./AmazeUI-2.4.2/assets/css/amazeui.min.css"/>
    <link href="./css/dlstyle.css" rel="stylesheet" type="text/css">
    <script src="./AmazeUI-2.4.2/assets/js/jquery.min.js"></script>
    <script src="./AmazeUI-2.4.2/assets/js/amazeui.min.js"></script>

    <!-- 引入vue -->
    <script src="js/plugins/vue/dist/vue.js"></script>
    <!-- 引入axios -->
    <script src="js/plugins/axios/dist/axios.js"></script>
    <!-- 配置axios的基础路径和全局属性 -->
    <script src="js/common.js"></script>

</head>

<body>

<div class="login-boxtitle">
    <a href="home/demo.html"><img alt="" src="./images/logobig.png"/></a>
</div>

<div class="res-banner" id="userRegister">
    <div class="res-main">
        <div class="login-banner-bg"><span></span><img src="./images/big.jpg"/></div>
        <div class="login-box">
            <div class="am-tabs" id="doc-my-tabs">
                <ul class="am-tabs-nav am-nav am-nav-tabs am-nav-justify">
                    <li class="am-active"><a href="">邮箱注册</a></li>
                    <li><a href="">手机号注册</a></li>
                </ul>

                <div class="am-tabs-bd">
                    <div class="am-tab-panel am-active">
                        <form method="post">

                            <div class="user-email">
                                <label for="email"><i class="am-icon-envelope-o"></i></label>
                                <input type="email" name="" id="email" v-model="emailUserForm.email" placeholder="请输入邮箱账号">
                            </div>

                            <!-- 图形验证码 -->
                            <div class="user-email">
                                <label for="e-imageCode"><i class="am-icon-check am-icon-sm"></i></label>
                                <input type="text" name="" style="width: 180px;" v-model="emailUserForm.imageCode" id="e-imageCode" placeholder="请输入图片验证码">
                                <img id="e-captcha-image-temp" @click="getImageCode":src="Base64imageCode"  class="captcha-image-temp"  alt="点击换图" title="点击换图" style="vertical-align: middle; cursor: pointer;">
                            </div>

                            <div class="user-pass">
                                <label for="password"><i class="am-icon-lock"></i></label>
                                <input type="password" name="" id="pwd" v-model="emailUserForm.password" placeholder="设置密码">
                            </div>
                            <div class="user-pass">
                                <label for="passwordRepeat"><i class="am-icon-lock"></i></label>
                                <input type="password" name="" id="pwdRepeat" v-model="emailUserForm.passwordRepeat" placeholder="确认密码">
                            </div>

                        </form>

                        <div class="login-links">
                            <label for="reader-me">
                                <input id="e-reader-me" type="checkbox"> 点击表示您同意商城《服务协议》
                            </label>
                        </div>
                        <div class="am-cf">
                            <input type="submit" name="" value="注册" @click="emailRegister" class="am-btn am-btn-primary am-btn-sm am-fl">
                        </div>

                    </div>

                    <div class="am-tab-panel">
                        <form method="post">
                            <div class="user-phone">
                                <label for="phone"><i class="am-icon-mobile-phone am-icon-md"></i></label>
                                <input type="tel" name="" id="phone" v-model="phoneUserForm.phone" placeholder="请输入手机号">
                            </div>
                            <!-- 图形验证码 -->
                            <div class="user-phone">
                                <label for="imageCode"><i class="am-icon-check am-icon-sm"></i></label>
                                <input type="text" name="" style="width: 180px;" v-model="phoneUserForm.imageCode" id="imageCode" placeholder="请输入图片验证码">
                                <img id="captcha-image-temp" @click="getImageCode":src="Base64imageCode"  class="captcha-image-temp"  alt="点击换图" title="点击换图" style="vertical-align: middle; cursor: pointer;">
                            </div>

                            <div class="verification">
                                <label for="code"><i class="am-icon-code-fork"></i></label>
                                <input type="tel" name="" id="code" v-model="phoneUserForm.phoneCode" placeholder="请输入验证码">
                                <button type="button" id="dyMobileButton" @click="sendMobileCode">获取</button>
                            </div>
                            <div class="user-pass">
                                <label for="password"><i class="am-icon-lock"></i></label>
                                <input type="password" name="" id="password" v-model="phoneUserForm.password" placeholder="设置密码">
                            </div>
                            <div class="user-pass">
                                <label for="passwordRepeat"><i class="am-icon-lock"></i></label>
                                <input type="password" name="" id="passwordRepeat"
                                       v-model="phoneUserForm.passwordRepeat" placeholder="确认密码">
                            </div>
                        </form>
                        <div class="login-links">
                            <label for="reader-me">
                                <input id="reader-me" type="checkbox"> 点击表示您同意商城《服务协议》
                            </label>
                        </div>
                        <div class="am-cf">
                            <input type="submit" name="" value="注册" @click="phoneRegister" class="am-btn am-btn-primary am-btn-sm am-fl">
                        </div>

                        <hr>
                    </div>

                    <script>
                        $(function () {
                            $('#doc-my-tabs').tabs();
                        })
                    </script>

                </div>
            </div>
        </div>
    </div>

    <div class="footer ">
        <div class="footer-hd ">
            <p>
                <a href="# ">恒望科技</a>
                <b>|</b>
                <a href="# ">商城首页</a>
                <b>|</b>
                <a href="# ">支付宝</a>
                <b>|</b>
                <a href="# ">物流</a>
            </p>
        </div>
        <div class="footer-bd ">
            <p>
                <a href="# ">关于恒望</a>
                <a href="# ">合作伙伴</a>
                <a href="# ">联系我们</a>
                <a href="# ">网站地图</a>
                <em>© 2015-2025 Hengwang.com 版权所有. 更多模板 <a href="http://www.cssmoban.com/" target="_blank" title="模板之家">模板之家</a>
                    - Collect from <a href="http://www.cssmoban.com/" title="网页模板" target="_blank">网页模板</a></em>
            </p>
        </div>
    </div>
</div>

<script type="text/javascript">
    new Vue({
        el: "#userRegister",
        data: {
            Base64imageCode: '',                            // base64图形验证码
            imageCodePrefix: "data:image/jpeg;base64,",     // 图形验证码显示的前缀

            // 手机号注册模型数据
            phoneUserForm: {
                phone: '',        // 输入的手机号
                imageCode: '',    // 输入的图形验证码
                phoneCode: "",    // 手机验证码
                password: "",          // 密码
                passwordRepeat: ""    // 确认密码
            },

            // 邮箱注册模型数据
            emailUserForm: {
                email: '',        // 输入的邮箱
                imageCode: '',    // 输入的图形验证码
                password: "",          // 密码
                passwordRepeat: ""    // 确认密码
            }

        },
        methods: {
            checkEmail(xxxmail) {
                // 简易验证邮箱的正则表达式 （超随意）
                let a = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
                return a.test(xxxmail)
            },
            createUuid(){
                var s = [];
                var hexDigits = "0123456789abcdefghi";
                for (var i = 0; i < 36; i++) {
                    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
                }
                s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
                s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
                s[8] = s[13] = s[18] = s[23] = "-";

                var uuid = s.join("");
                return uuid;
            },

            // 1. 获取图形验证码
            getImageCode() {
                // 1. 从localStorage中获取key：phoneRegisterCodeKey
                var key = localStorage.getItem("phoneRegisterCodeKey");
                // 2. 判断： 为空就重新生成 , 不为空就直接使用 - 让key唯一
                if(!key) { // js可以直接判断 ！key 就是key没有值
                    key = this.createUuid(); // 没有就调用uuid给他创建一个值
                    localStorage.setItem("phoneRegisterCodeKey", key); // 然后将这个值给phoneRegisterCodeKey
                }
                // 3. 向后端发送异步请求 并携带key
                this.$http.get("/verifyCode/image/" + key).then(res => {
                    // 4. 在回调函数中获取图形验证码的base64值 拼接上前缀 绑定给src
                    this.Base64imageCode = this.imageCodePrefix + res.data.resultObj;
                })
            },

            // 2. 发送手机验证码
            sendMobileCode(){
                //1 . 判断手机号不为空
                if(!this.phoneUserForm.phone){
                    alert("手机号不能为空");
                    return;
                }
                // 2. 判断图片验证码不为空
                if(!this.phoneUserForm.imageCode){
                    alert("图片验证码不能为空");
                    return;
                }
                // 3. 获取按钮，禁用按钮  发送时灰化不能使用，发送成功倒计时60才能使用，如果发送失败立即可以发送
                var sendBtn = $(event.target);
                sendBtn.attr("disabled",true);

                var param = { // 需要传递的参数 电话(发短信) - 图形验证码(做比对) - (图形码key - 后端从redis里查value对比传过来的图码值)
                    phone: this.phoneUserForm.phone,
                    imageCode: this.phoneUserForm.imageCode,
                    imageCodeKey: localStorage.getItem("phoneRegisterCodeKey")
                };

                // 4.发送ajax请求 - 这里传递的参数我们没有可接收的类 写一个Dto类 - 数据传输对象类
                this.$http.post("/verifyCode/smsCode",param).then(res=>{
                    var ajaxResult = res.data;
                    if(ajaxResult.success){
                        alert("手机验证码已经发送到您的手机，请在3分钟内使用");
                        // 4.1.将按钮的html内容 设置成倒计时
                        var time = 60;
                        // window.setInterva(执行方法， 周期) - 周期性定时器
                        var interval = window.setInterval( function () {
                            // 每一条倒计时减一
                            time = time - 1 ;
                            // 把倒计时时间搞到按钮上
                            sendBtn.html(time);
                            // 4.2.倒计时完成恢复按钮
                            if(time <= 0){
                                // 设置可用 且 文本为 重新发送
                                sendBtn.html("重发");
                                sendBtn.attr("disabled",false);
                                // 清除定时器
                                window.clearInterval(interval);
                            }
                        },1000);
                    }else{
                        // 4.3.发送失败：提示，恢复按钮
                        sendBtn.attr("disabled",false);
                        alert("发送失败:"+ajaxResult.message);
                    }
                })
            },

            // 3. 手机号注册 点击注册触发
            phoneRegister() {
                // 1. 判断手机号不为空
                if(!this.phoneUserForm.phone){
                    alert("手机号不能为空");
                    return;
                }
                // 2. 发送请求
                this.$http.post("/user/phone/register", this.phoneUserForm).then(res => {
                    if (res.data.success) {
                        // 注册成功跳转登录页面
                        location.href = "/login.html"
                    } else {
                        alert(res.data.message)
                    }
                }).catch(res => {
                    alert("系统错误！")
                })
            },

            emailRegister() {
                //1 . 简单验证邮箱格式是否正确
                if(!this.checkEmail(this.emailUserForm.email)){
                    alert("请输入正确的邮箱");
                    return;
                }
                // 2. 判断图片验证码不为空
                if(!this.emailUserForm.imageCode){
                    alert("图片验证码不能为空");
                    return;
                }
                var param = { // 需要传递的参数 电话(发短信) - 图形验证码(做比对) - (图形码key - 后端从redis里查value对比传过来的图码值)
                    imageCode: this.emailUserForm.imageCode,
                    imageCodeKey: localStorage.getItem("phoneRegisterCodeKey")
                };

                // 4.发送ajax请求 - 这里传递的参数我们没有可接收的类 写一个Dto类 - 数据传输对象类
                this.$http.post("/verifyCode/emailCode",param).then(res=> {
                    if (res.data.success) {
                        this.$http.post("/user/email/register", this.emailUserForm).then(res => {
                            if (res.data.success) {
                                alert("注册成功，请通过邮箱链接激活！")
                                // 注册成功跳转登录页面
                                location.href = "/login.html"
                            } else {
                                alert(res.data.message)
                            }
                        })
                    } else {
                        alert(res.data.message)
                    }
                })
            }
        },
        mounted() {
            // 页面一加载获取图形验证码
            this.getImageCode();
        }
    })
</script>

</body>

</html>